/**
 * @author benny.zheng
 * @data 2016-06-06
 * @description 本文件用于方便复制粘贴UI模块之用，请更新这里的说明
 *              另外，考虑到一般是放在js/src/pages/page-name/main.js，因此使用../../
 *              如果不是这个目录，请更改成正确的相对路径
 */
//----------------require--------------

var viewport = require("mlib/dom/viewport"); // viewport
var base = require("mlib/comp/base"); // 基础对象
var parseModule = require("mlib/dom/parseModule"); // 页面模块自动解析
var wechat = require("../plugin/wx/wx-api");

var ajax = require("mlib/io/ajax");
var closest = require("mlib/dom/closest");
var virtualLink = require("mlib/util/virtualLink");
var follow = require("../public/js/follow");
var error = require("../public/js/error");
var date = require("../plugin/util/date");
var Alert = require("../layer/weuialert");
var touch = require("mlib/evt/touch");
var parseUrl = require("../plugin/url/parse-url");

var render = require("./main.ejs"); // 页面总模板


module.exports = function(node, opts) {
    //-----------声明模块全局变量-------------
    var nodeList = null; // 存储所有关键节点
    var that = base();
    var data = null;

    var receiveUrl = null;
    var m_cardid = null;
    var urlParams = null;
	
	
    //-------------事件响应声明---------------
    var evtFuncs = {
        getVoucher:function(evt){
            follow();
        },
    	    getTicket:function(evt){
	    	    	if(this.classList.contains("disabled")){
	    	    		return ;
	    	    	}
                    this.classList.add("disabled");
					ajax({
						url:receiveUrl,
						data:{"cardId":m_cardid},
						method: "post",
						onSuccess: function(result) {
                           if(result.status>0){
	                           	if(result.data.length>0){
									location.href=result.data[0].detailRouter;                            
	                           	}                           	
                           }else{
                           		Alert(result.msg);
                           }
							 				
						},
						onError: function(xmlHttp) {
						   that.unLock();
						}
					});
    	    }
	}

    //-------------子模块实例化---------------
    var initMod = function() { 
        urlParams =  parseUrl();
		custFuncs.getdata();
         virtualLink(function(evt){
         	location.href = evt.target.getAttribute("data-href");
         });
    }

    //-------------绑定事件------------------
    var bindEvents = function() {
			touch.on(document.body, 'tap', ".followme", evtFuncs.getVoucher);
			touch.on(document.body, 'tap', "#getTicket", evtFuncs.getTicket);
	}

    //-------------自定义函数----------------
    var custFuncs = {
		    getdata:function(){
					ajax({
						url:data.requestAPIUrl,
						data:{},
						method: "get",
						onSuccess: function(result) {
							var respdata = result.data;
							if(typeof(respdata)!="undefined"){
                                if(respdata.cardStatus>0 && respdata.cardStatus<3){                                
    							   respdata.imgDomain = data.imgDomain;
    						 	   document.body.insertAdjacentHTML('AfterBegin', render(respdata));
/*								   
                                   if(respdata.canShared==1){
                                        wechat('open',{
                                            "title":"您的朋友分享给您一张优惠券",
                                            "link":location.origin+data.shareUrl,
                                            "imgUrl":respdata.imgDomain +"/weixin/couponLog.jpg",
                                            "desc":respdata.title,
                                            "success":function (res) {
												urlParams.shareCode = "card";
												ajax({
													url: "/index.php?r=CouponApi/data/statistics",
													data:urlParams, 
													method: "post",
													onSuccess: function(result) {
													},
													onError: function(xmlHttp) {
													}
												});	
                                            }
    									}).init();
                                   }else{
                                   		wechat('off').init();
                                   }
*/
								   wechat('off').init();
                                   receiveUrl= respdata.receiveUrl;
                                   m_cardid= respdata.cardId;
                               }else{
                                    error('error'); 
                               }
							}else{
								error('error');	
							}
	
						},
						onError: function(xmlHttp) {
						   that.unLock();
						}
					});
			}

    }

    //-------------一切从这开始--------------
    var init = function(_data) {

        data = _data;

        // 找到所有带有node-name的节点
        nodeList = parseModule(node);
        // 子模块实例化
        initMod();
        // 绑定事件
        bindEvents();


    }

    //---------------暴露API----------------
    that.init = init;

    return that;
};